﻿/* ==============================================================================
 * 功能描述：BaseModule  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/4 11:04:02
 * ==============================================================================*/
using UnityEngine;
using System.Collections;

namespace ZZQFrameWork
{
    public class BaseModule
    {
        /// <summary>
        /// 模块状态改变事件
        /// </summary>
        public event OnModuleStateChangedDe OnModuleStateChangedEvent;

        /// <summary>
        /// 模块状态
        /// </summary>
        private Enum_ModuleState state = Enum_ModuleState.Init;

        public Enum_ModuleState State
        {
            get { return state; }
            set
            {
                if (state != value)
                {
                    Enum_ModuleState oldState = state;
                    state = value;
                    OnModuleStateChanged(oldState, state);
                    if (OnModuleStateChangedEvent != null)
                    {
                        OnModuleStateChangedEvent(oldState, state);
                    }
                }
            }
        }

        /// <summary>
        /// 模块状态改变
        /// </summary>
        /// <param name="oldState"></param>
        /// <param name="newState"></param>
        protected virtual void OnModuleStateChanged(Enum_ModuleState oldState, Enum_ModuleState newState)
        {

        }

        /// <summary>
        /// 模块注册类型
        /// </summary>
        private Enum_ModuleRegisterType registerType = Enum_ModuleRegisterType.NotRegister;

        /// <summary>
        /// 模块是否自动注册
        /// </summary>
        public bool AutoRegister
        {
            get { return registerType == Enum_ModuleRegisterType.AutoRegister; }
            set
            {
                if (registerType == Enum_ModuleRegisterType.NotRegister ||
                    registerType == Enum_ModuleRegisterType.AutoRegister)
                {
                    registerType = value ? Enum_ModuleRegisterType.AutoRegister : Enum_ModuleRegisterType.NotRegister;
                }
            }
        }

        /// <summary>
        /// 模块是否注册
        /// </summary>
        public bool HasRegister
        {
            get { return registerType == Enum_ModuleRegisterType.AlreadyRegister; }
        }

        /// <summary>
        /// 加载模块
        /// </summary>
        public void Load()
        {
            if (State != Enum_ModuleState.Init) return;
            State = Enum_ModuleState.Loading;
            if (AutoRegister)
            {
                //在moduleManager中注册该模块
                ModuleManager.Instance.RegisterModule(this);
            }
            OnLoad();
            State = Enum_ModuleState.Ready;
        }

        /// <summary>
        /// 释放模块
        /// </summary>
        public void Release()
        {
            if (State != Enum_ModuleState.Disable)
            {
                State = Enum_ModuleState.Disable;
                if (registerType == Enum_ModuleRegisterType.AlreadyRegister)
                {
                    //从moduleManager中注销该模块
                    ModuleManager.Instance.UnRegisterModule(this);
                }
                OnRelease();
                State = Enum_ModuleState.Release;
            }
        }

        protected virtual void OnLoad()
        {

        }

        protected virtual void OnRelease()
        {

        }
    }
}